package com.jl.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jl.commons.base.BaseController;
import com.jl.commons.constant.Constants;
import com.jl.commons.constant.ErrorCode;
import com.jl.commons.constant.ErrorMessage;
import com.jl.commons.result.PageDataInfo;
import com.jl.commons.result.ResultData;
import com.jl.commons.utils.BASE64;
import com.jl.model.DevicePart;
import com.jl.service.IDevicePartService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author 江剑
 */
@Controller
@RequestMapping("/devicePart")
public class DevicePartController extends BaseController {

    @Autowired
    private IDevicePartService devicePartService;

    @GetMapping("/devicePartDataGrid")
    @RequiresPermissions("/devicePart/devicePartDataGrid")
    public String keyWordList(){
        return "devicePart_list";
    }


    @GetMapping("/add")
    @RequiresPermissions("/devicePart/add")
    public String addDevicePart(ModelMap mp, Long id){
        mp.put("id",id);
        return "devicePart_add";
    }

    /**
     * 一定是add
     * @param json
     * @return
     */
    @PostMapping("/add")
    @RequiresPermissions("/devicePart/add")
    @ResponseBody
    public ResultData addDevicePart(String json){
        String value = BASE64.decode(json);
        if(null == value){
            return fail(ErrorCode.BASE64_DECODER_ERROR, ErrorMessage.BASE64_DECODER_ERROR);
        }
        DevicePart devicePart = JSON.parseObject(value,DevicePart.class);
        //判断名称是否重复，不能拥有重复名称
        if(devicePartService.isDevicePartUsed(devicePart.getName())){
            return fail(ErrorCode.DEVICE_NUMBER_USED, ErrorMessage.DEVICE_NUMBER_USED);
        }
        devicePart.setCreateId(getUserId());
        devicePart.setGmtCreate(new Date());
    //    devicePartService.saveDevicePartWithComponents(devicePart);
        return success(devicePart);
    }

    @GetMapping("/edit")
    @RequiresPermissions("/devicePart/edit")
    public String editDevicePart(ModelMap mp, Long id){
        mp.put("id",id);
        return "devicePart_add";
    }

    /**
     * 一定是add
     * @param json
     * @return
     */
    @PostMapping("/edit")
    @RequiresPermissions("/devicePart/edit")
    @ResponseBody
    public ResultData editDevicePart(String json){
        String value = BASE64.decode(json);
        if(null == value){
            return fail(ErrorCode.BASE64_DECODER_ERROR, ErrorMessage.BASE64_DECODER_ERROR);
        }
        DevicePart devicePart = JSON.parseObject(value,DevicePart.class);

        boolean canUse = devicePartService.isDevicePartCanUse(devicePart.getName(),devicePart.getId());
        if(!canUse){
            return fail(ErrorCode.DEVICE_NUMBER_USED,ErrorMessage.DEVICE_NUMBER_USED);
        }
  //      devicePartService.updateDevicePartWithComponents(getShiroUser(),devicePart);
        return success(devicePart);
    }

    @PostMapping("/delete")
    @RequiresPermissions("/devicePart/delete")
    @ResponseBody
    public ResultData deletesDevicePart(String json){
        String value = BASE64.decode(json);
        if(null == value){
            return fail(ErrorCode.BASE64_DECODER_ERROR, ErrorMessage.BASE64_DECODER_ERROR);
        }
        List<Long> ids = JSONArray.parseArray(value,Long.class);
      //  boolean b = devicePartService.deleteDevicePartByIds(ids);
        boolean b = true;
        if(b){
            return success();
        }
        return fail();
    }

    /**
     * 一定是dataGrid
     * @param current
     * @param pageSize
     * @param searchCol
     * @param searchValue
     * @return
     */
    @PostMapping("/dataGrid")
    @RequiresPermissions("/devicePart/dataGrid")
    @ResponseBody
    public ResultData devicePartDataGrid(Long current,Long pageSize,String searchCol,String searchValue){
        if(current == null || pageSize == null){
            return fail(ErrorCode.PAGE_PARAM_ERROR, ErrorMessage.PAGE_PARAM_ERROR);
        }
        QueryWrapper<DevicePart> queryWrapper = new QueryWrapper<>();
        //目前的查询只有单表
        if(StringUtils.isNotBlank(searchValue) && StringUtils.isNotBlank(searchCol)){
            if(searchCol.equals(Constants.ID)){
                queryWrapper.eq("t.id",searchValue);
            }else{
                queryWrapper.like("t."+searchCol,searchValue);
            }
        }
        PageDataInfo<Map<String,Object>> pageDataInfo =new PageDataInfo<>();
        pageDataInfo.setCurrent(current);
        pageDataInfo.setSize(pageSize);

        devicePartService.selectDevicePartDataGrid(pageDataInfo,queryWrapper);
        return success(pageDataInfo);
    }


    @PostMapping("/dataGridByIds")
    @RequiresPermissions("/devicePart/dataGrid")
    @ResponseBody
    public ResultData devicePartDataGridByIds(String json){
        String value = BASE64.decode(json);
        if(null == value){
            return fail(ErrorCode.BASE64_DECODER_ERROR, ErrorMessage.BASE64_DECODER_ERROR);
        }
        List<Long> ids = JSONArray.parseArray(value,Long.class);
        PageDataInfo<Map<String,Object>> pageDataInfo =new PageDataInfo<>();
        pageDataInfo.setCurrent(Constants.FIRST_PAGE);
        pageDataInfo.setSize(Constants.MAX_PAGE_SIZE);
        devicePartService.selectDevicePartDataGridByIds(pageDataInfo,ids);
        return success(pageDataInfo);
    }

}